//
//  WBVisitorView.swift
//  微博(swift)
//
//  Created by 汤米 on 16/4/18.
//  Copyright © 2016年 汤米. All rights reserved.
//

import UIKit
import SnapKit

class WBVisitorView: UIView {
    
    var visitorClosure: (() -> ())?

    // MARK: -  重写 init frame 方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setUpUI()
    }
    
    // MARK: -  重写 init frame 方法 的时候   必须要实现以下方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  外界调用方法
    func setImageinfo(imageName: String?, title: String?) {
        
        if let image = imageName, text = title {
            
            circleImageView.hidden = true
            pictureImageView.image = UIImage (named: image)
            descLabel.text = text
        }else{
            
            circleAnimate()

        }
    }
    
    // MARK: -   圆圈动画
    private func circleAnimate() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.duration = 15
        animation.repeatCount = MAXFLOAT
        animation.toValue = 2 * M_PI
        animation.removedOnCompletion = false
        
        circleImageView.layer.addAnimation(animation, forKey: nil)
    }
    
    // MARK: -  设置控件frame
    override func layoutSubviews() {
        
        circleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
            
        }
        
        coverImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        
        pictureImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        
        descLabel.snp_makeConstraints { (make) -> Void in

            make.top.equalTo(coverImageView.snp_bottom).offset(16)
            make.height.equalTo(36)
            make.centerX.equalTo(self)
            make.width.equalTo(230)
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(descLabel.snp_bottom).offset(16)
            make.left.equalTo(descLabel)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(descLabel.snp_bottom).offset(16)
            make.right.equalTo(descLabel)
            make.size.equalTo(registerButton)
        }

    }
    
    // MARK: -  设置界面控件
    private func setUpUI() {
        
        self.backgroundColor = RGB(red: 237, green: 237, blue: 237)
        
        addSubview(circleImageView)
        addSubview(coverImageView)
        addSubview(pictureImageView)
        addSubview(descLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
       
    }
    
    // MARK: -  注册.登录按钮的点击事件
    func login() {
        
        visitorClosure?()
    }
    
    // MARK: -  懒加载控件
    // MARK: -  圆圈控件
    private lazy var circleImageView: UIImageView = {
        
        let cicleImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return cicleImageView
    }()
    
    // MARK: -  遮挡控件
    private lazy var coverImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // MARK: -  中心图片控件
    private lazy var pictureImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    // MARK: -  文字label
    private lazy var descLabel: UILabel = {
        
        let descLabel = UILabel(textColor: UIColor.grayColor(), textFont: 14)
        descLabel.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        descLabel.numberOfLines = 0
        descLabel.textAlignment = .Center
        
        return descLabel
    }()
    
    // MARK: -  注册按钮
    private lazy var registerButton: UIButton = {
       
        let registerButton = UIButton()
        
        registerButton.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        registerButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        registerButton.setTitle("注册", forState: UIControlState.Normal)
        registerButton.titleLabel?.font = UIFont.systemFontOfSize(14)
        registerButton.setTitleColor(themeColor, forState: UIControlState.Normal)
        
        
        return registerButton
    }()
    
    // MARK: -  登录按钮
    private lazy var loginButton: UIButton = {
        
        let loginButton = UIButton()
        
        loginButton.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        loginButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        loginButton.setTitle("登录", forState: UIControlState.Normal)
        loginButton.titleLabel?.font = UIFont.systemFontOfSize(14)
        loginButton.setTitleColor(themeColor, forState: UIControlState.Normal)
        
        
        return loginButton
        }()
}
